package com.example.administrator.httpclient;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

/**
 * Created by Administrator on 2017/4/7.
 */

public class StreamTools {
    public static String readStream(InputStream is){
        try{
            ByteArrayOutputStream baos=new ByteArrayOutputStream();
            byte[] buffer=new byte[1024];
            int len=-1;
            while(is.read(buffer)!=-1){
                baos.write(buffer,0,len);
            }
            is.close();
            String temptext=new String(baos.toByteArray());
            if (temptext.contains("charset=GBK")){
                return new String(baos.toByteArray(),"GBK");
            }else{
                return new String(baos.toByteArray(),"UTF-8");
            }

        }catch (IOException e){
            e.printStackTrace();
            return null;

        }
    }
}
